using System;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

namespace BCW.GameTypes.SkyBlock
{
	public class SkyBlockController : MonoBehaviour, IPunObservable
	{
		public const int STARS_FOR_DELIVERING_FLAG = 3;

		public const int STARS_FOR_CAPTURE_POINT = 15;

		public static SkyBlockController instance;

		public List<GameObject> roadRed = new List<GameObject>();

		public List<GameObject> roadBlue = new List<GameObject>();

		public Material roadRedMaterial;

		public Material roadBlueMaterial;

		private SkyBlockBlocator blocator;

		private PhotonView photonView;

		public Color redColor = Color.red;

		public Color blueColor = Color.magenta;

		public List<SkyFlagDropZone> dropZones = new List<SkyFlagDropZone>();

		public List<SkyFlag> flags = new List<SkyFlag>();

		private Dictionary<PunTeams.Team, List<GameObject>> road = new Dictionary<PunTeams.Team, List<GameObject>>
		{
			{
				PunTeams.Team.red,
				new List<GameObject>()
			},
			{
				PunTeams.Team.blue,
				new List<GameObject>()
			}
		};

		private Dictionary<PunTeams.Team, int> index = new Dictionary<PunTeams.Team, int>
		{
			{
				PunTeams.Team.red,
				-1
			},
			{
				PunTeams.Team.blue,
				-1
			}
		};

		public bool _hasFlag;

		private float _coefficient;

		private PunTeams.Team leaderTeam;

		private PointerPanel pointer;

		private int _countCaptureNotification = 2;

		private int _countDropNotification = 2;

		public bool hasFlag
		{
			get
			{
				return _hasFlag;
			}
			set
			{
				if (value)
				{
					SkyFlagDropZone skyFlagDropZone = dropZones.Find((SkyFlagDropZone x) => x.team == TDMController.myTeam);
					if (skyFlagDropZone != null)
					{
						pointer.target = skyFlagDropZone.transform;
					}
				}
				else
				{
					SkyFlag skyFlag = flags.Find((SkyFlag x) => x.team == TDMController.enemyTeam);
					if (skyFlag != null && pointer != null)
					{
						pointer.target = skyFlag.transform;
					}
				}
				_hasFlag = value;
			}
		}

		public static event Action<PunTeams.Team, bool> OnSwitchFlag;

		private void Awake()
		{
			instance = this;
			road[PunTeams.Team.blue] = roadBlue;
			road[PunTeams.Team.red] = roadRed;
			photonView = GetComponent<PhotonView>();
			SkyFlag.OnFlagCollision += OnFlagCollision;
			SkyFlagDropZone.OnDropZoneCollision += OnDropZoneCollision;
		}

		public void InitPointer()
		{
			if (pointer == null)
			{
				pointer = PointerController.instance.AddPointer(flags.Find((SkyFlag x) => x.team == TDMController.enemyTeam).transform, 0f);
			}
			else
			{
				pointer.target = flags.Find((SkyFlag x) => x.team == TDMController.enemyTeam).transform;
			}
		}

		private void OnDestroy()
		{
			SkyFlag.OnFlagCollision -= OnFlagCollision;
			SkyFlagDropZone.OnDropZoneCollision -= OnDropZoneCollision;
			instance = null;
		}

		public float GetScore(PunTeams.Team team)
		{
			return (float)Mathf.Clamp(index[team] + 1, 0, road[team].Count) / (float)road[team].Count;
		}

		public int GetCountBlocks(PunTeams.Team team)
		{
			return Mathf.Clamp(index[team] + 1, 0, road[team].Count);
		}

		public bool GetRoadState(PunTeams.Team team)
		{
			return index[team] + 1 == road[team].Count;
		}

		public void UpdateColors()
		{
			roadRedMaterial.color = ((TDMController.myTeam != PunTeams.Team.red) ? redColor : blueColor);
			roadBlueMaterial.color = ((TDMController.myTeam != PunTeams.Team.blue) ? redColor : blueColor);
			foreach (SkyFlag flag in flags)
			{
				Renderer[] componentsInChildren = flag.GetComponentsInChildren<Renderer>();
				Renderer[] array = componentsInChildren;
				foreach (Renderer renderer in array)
				{
					renderer.material.color = ((TDMController.myTeam != flag.team) ? blueColor : redColor);
				}
			}
			foreach (SkyFlagDropZone dropZone in dropZones)
			{
				Renderer[] componentsInChildren2 = dropZone.GetComponentsInChildren<Renderer>();
				Renderer[] array2 = componentsInChildren2;
				foreach (Renderer renderer2 in array2)
				{
					renderer2.material.color = ((TDMController.myTeam != dropZone.team) ? redColor : blueColor);
				}
			}
		}

		public void IncreaseScore(PunTeams.Team team, int val)
		{
			if (val < 0 || index[team] >= road[team].Count - 1)
			{
				return;
			}
			if (PhotonNetwork.isMasterClient)
			{
				TDMController.instance.AddTeamPoints(team, val);
			}
			int num = index[team] + val;
			while (index[team] < num && index[team] < road[team].Count - 1)
			{
				Dictionary<PunTeams.Team, int> dictionary;
				Dictionary<PunTeams.Team, int> dictionary2 = (dictionary = index);
				PunTeams.Team key;
				PunTeams.Team key2 = (key = team);
				int num2 = dictionary[key];
				dictionary2[key2] = num2 + 1;
				road[team][index[team]].SetActive(true);
			}
			if (GetRoadState(team))
			{
				string empty = string.Empty;
				if (team == TDMController.myTeam)
				{
					hasFlag = false;
					PlayerBehavior.MyPlayer.SwitchFlag(false);
					pointer.target = BCWFlagManager.instance.transform;
					empty = LocalizationManager.GetTermTranslation("Game_Common/sm_SkyBlock_yourTeamRoadComplete");
				}
				else
				{
					empty = LocalizationManager.GetTermTranslation("Game_Common/sm_SkyBlock_enemyTeamRoadComplete");
				}
				GameplayNotificationController.instance.AddNotification(empty, 1);
			}
			PunTeams.Team team2 = leaderTeam;
			if (index[PunTeams.Team.blue] != index[PunTeams.Team.red])
			{
				team2 = ((index[PunTeams.Team.blue] <= index[PunTeams.Team.red]) ? PunTeams.Team.red : PunTeams.Team.blue);
			}
			if (leaderTeam != team2)
			{
				string empty2 = string.Empty;
				empty2 = ((team2 != TDMController.myTeam) ? LocalizationManager.GetTermTranslation("Game_Common/sm_SkyBlock_yourTeamBehind") : LocalizationManager.GetTermTranslation("Game_Common/sm_SkyBlock_yourTeamOnward"));
				GameplayNotificationController.instance.AddNotification(empty2, 2);
			}
			leaderTeam = team2;
			GameInfoWindow.instance.scrollsTdmScore.UpdateScores();
		}

		public void Reset()
		{
			index[PunTeams.Team.red] = -1;
			index[PunTeams.Team.blue] = -1;
			hasFlag = false;
			leaderTeam = PunTeams.Team.none;
			PlayerBehavior.MyPlayer.SwitchFlag(false);
			foreach (GameObject item in roadRed)
			{
				item.SetActive(false);
			}
			foreach (GameObject item2 in roadBlue)
			{
				item2.SetActive(false);
			}
			GameInfoWindow.instance.scrollsTdmScore.UpdateScores();
			SkyBlockStatistics.ResetStatitics();
		}

		private void OnDropZoneCollision(PunTeams.Team zone)
		{
			if (hasFlag && zone == TDMController.myTeam && PVPManager.instance.gameIsStarted && !GetRoadState(TDMController.myTeam))
			{
				hasFlag = false;
				PlayerBehavior.MyPlayer.SwitchFlag(false);
				CupsManager.Cups += 3;
				ShowCaptureFlagNotification();
				photonView.RPC("CaptureEnemyFlag", PhotonTargets.All, TDMController.myTeam);
			}
		}

		private void OnFlagCollision(PunTeams.Team flag)
		{
			if (flag != TDMController.myTeam && PVPManager.instance.gameIsStarted && !GetRoadState(TDMController.myTeam))
			{
				hasFlag = true;
				ShowDropFlagNotification();
				PlayerBehavior.MyPlayer.SwitchFlag(true);
				SkyBlockStatistics.blocksPickedUp++;
			}
		}

		[PunRPC]
		private void CaptureEnemyFlag(PunTeams.Team team)
		{
			if (PhotonNetwork.isMasterClient)
			{
				IncreaseScore(team, 1);
			}
			SkyBlockStatistics.blocksStacked++;
			string empty = string.Empty;
		}

		public void ShowCaptureFlagNotification()
		{
			if (_countCaptureNotification > 0)
			{
				string termTranslation = LocalizationManager.GetTermTranslation("Game_Common/sm_SkyBlock_takeBlock");
				GameplayNotificationController.instance.AddNotification(termTranslation, 99, Color.white);
				_countCaptureNotification--;
			}
		}

		public void ShowDropFlagNotification()
		{
			if (_countDropNotification > 0)
			{
				string termTranslation = LocalizationManager.GetTermTranslation("Game_Common/sm_SkyBlock_returnBlock");
				GameplayNotificationController.instance.AddNotification(termTranslation, 99, Color.white);
				_countDropNotification--;
			}
		}

		public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
		{
			if (stream.isWriting)
			{
				stream.SendNext(index[PunTeams.Team.red]);
				stream.SendNext(index[PunTeams.Team.blue]);
			}
			else
			{
				IncreaseScore(PunTeams.Team.red, (int)stream.ReceiveNext() - index[PunTeams.Team.red]);
				IncreaseScore(PunTeams.Team.blue, (int)stream.ReceiveNext() - index[PunTeams.Team.blue]);
			}
		}
	}
}
